set linesize 140
set rmsg on
set scheme s1mono
/***************************************************************************
	Project		:	Army Service in the All-Volunteer Era	
	Author(s)	:	Kyle Greenberg	(kyle.greenberg@westpoint.edu)
					Matthew Gudgeon (matthew.gudgeon@westpoint.edu)
					Adam Isen 		(Adam.Isen@treasury.gov)
					Corbin Miller 	(Corbin.Miller@treasury.gov)
					Rich Patterson 	(rich_patterson@byu.edu)
	File Name	:	table5.do
	Description	:	This file runs the analysis for table 5
****************************************************************************/

*---- if not running master, set up file structure ----*
*change directory to where programs and subfolders are stored
cd "~/army/QJE_rep/"

if "${raw}"==""		global raw		"raw/"
if "${data}"==""	global data		"data/"
if "${output}"==""	global output	"output/"

cap mkdir	"${output}"

*prep unemployment data
use "${raw}laus_quarterly_unemployment", clear
replace q=119+(year-1990)*4+quarter
keep q unemployment hor_state
save "${data}army_ui_qtr", replace

use "${raw}army_ready.dta", clear

keep if missc==0 

merge m:1 hor quarter using "${data}army_ui_qtr", keep(1 3)
sum unemployment
replace unem=r(mean) if _merge==1
drop _merge

foreach a in unemployment single med poor_share1990 emp2000 age_days emp {
	xtile b`a'=`a', n(5)
}

log using "${output}table5", text replace

*31 cutoff
quietly logit white i.bunemployment  male i.bage_days educ_lessthanhs educ_inhs educ_ged educ_hsdip educ_some_coll educ_coll_grad   i.firstafqt_fy   i.bemp i.bsingle i.bpoor_share1990 i.bmed if inrange(firstafqt,12,49) [pw=count_11_19]
predict nx
replace nx=1/(1-nx)*nx
gen nweight_full=nx*count_11_19
gen nweight_11_19=nx*count_11_19

ivregress 2sls earn_11_19 k31  instk31 k31_2 instk31_2 i.quarter  (access=inst31) if inrange(firstafqt,12,49) & white==0 & nweight_full!=.  [aw=nweight_full], r

drop nx nweight* 

*50 cutoff
quietly logit white i.bunemployment  male i.bage_days educ_lessthanhs educ_inhs educ_ged educ_hsdip educ_some_coll educ_coll_grad i.firstafqt_fy   i.bemp i.bsingle i.bpoor_share1990 i.bmed if inrange(firstafqt,31,68) [pw=count_11_19]
predict nx
replace nx=1/(1-nx)*nx
gen nweight_full=nx*count_11_19
gen nweight_11_19=nx*count_11_19

ivregress 2sls earn_11_19 k50  instk50 k50_2 instk50_2 i.quarter  (access=inst50) if inrange(firstafqt,31,68)   & white==0 & nweight_full!=. [aw=nweight_full], r 

drop nx nweight* 

*
use "${raw}army_ready.dta", clear

keep if missc==0 

merge 1:1 pid using "${raw}army_family.dta"
drop if _merge==2
gen dep=0
replace dep=1 if is_dep!=. & is_dep!=0
replace is_dep=0 if is_dep==.
replace poor=0 if poor==.
replace file=0 if file==.
replace single=0 if single==.
replace tot_inc=0 if tot_inc==.
gen young=0
replace young=1 if _merge==3
drop _merge

replace tot_inc=0 if tot_inc<0
replace tot_inc=250000 if tot_inc>250000

merge m:1 hor quarter using "${data}army_ui_qtr.dta"
drop if _merge==2
sum unemployment
replace unem=r(mean) if _merge==1
drop _merge

foreach a in singleparent_share1990 med poor_share1990 emp2000 age_days emp unemployment {
	xtile b`a'=`a', n(5)
}

xtile btot_inc=tot_inc if tot_inc!=0, n(4)
replace btot_inc=20 if tot_inc==0

quietly logit white i.bunemployment  i.btot_inc young file poor is_dep dep single male i.bage_days educ_lessthanhs educ_inhs educ_ged educ_hsdip educ_some_coll educ_coll_grad i.firstafqt_fy i.bemp i.bsingle i.bpoor_share1990 i.bmed  if  inrange(firstafqt,12,49) [pw=count_11_19]

predict nx
replace nx=1/(1-nx)*nx
gen nweight_full=nx*count_11_19
gen nweight_11_19=nx*count_11_19

ivregress 2sls earn_11_19 k31  instk31 k31_2 instk31_2 i.quarter  (access=inst31) if inrange(firstafqt,12,49) & white==0 & nweight_full!=.  [aw=nweight_full], r

drop nx nweight* 

quietly logit white i.bunemployment  i.btot_inc young file poor is_dep dep single male i.bage_days educ_lessthanhs educ_inhs educ_ged educ_hsdip educ_some_coll educ_coll_grad i.firstafqt_fy   i.bemp i.bsingle i.bpoor_share1990 i.bmed  if    inrange(firstafqt,31,68) [pw=count_11_19]
predict nx
replace nx=1/(1-nx)*nx
gen nweight_full=nx*count_11_19
gen nweight_11_19=nx*count_11_19

ivregress 2sls earn_11_19 k50  instk50 k50_2 instk50_2 i.quarter  (access=inst50) if inrange(firstafqt,31,68)   & white==0 & nweight_full!=. [aw=nweight_full], r 

drop nx nweight* b*

*
use "${raw}armyready.dta", clear
gen black=0
replace black=1 if white==0
drop if missc==1

merge 1:1 pid using "${raw}army_family.dta"
drop if earn_11_19==.

gen dep=0
replace dep=1 if is_dep!=. & is_dep!=0
replace is_dep=0 if is_dep==.
replace poor=0 if poor==.
replace file=0 if file==.
replace single=0 if single==.
replace tot_inc=0 if tot_inc==.
gen young=0
replace young=1 if _merge==3
drop _merge

replace tot_inc=0 if tot_inc<0
replace tot_inc=250000 if tot_inc>250000

foreach a in singleparent_share1990 med poor_share1990 emp2000 age_days emp {
	xtile b`a'=`a' , n(5)  
}

xtile btot_inc=tot_inc if tot_inc!=0 , n(4)
replace btot_inc=20 if tot_inc==0

gen a=10
replace a=1 if firstafqt==30
sort a
gen f=_n if a==1
gen p30=.
sum f
local aa=r(max)
quietly forvalues a = 1/`aa' {
	reg earn_11_19  male i.firstyear i.bage_days educ_lessthanhs educ_inhs educ_ged  educ_some_coll educ_coll_grad    i.bemp2000 i.bsingleparent_share1990 i.bpoor_share1990 i.bmed   if  firstafqt==30 & f!=`a' [aw=count_11]
	predict predict30 
	replace p30=predict30 if f==`a'
	drop predict30
}
quietly reg earn_11_19 male i.firstyear i.bage_days educ_lessthanhs educ_inhs educ_ged  educ_some_coll educ_coll_grad    i.bemp2000 i.bsingleparent_share1990 i.bpoor_share1990 i.bmed   if  firstafqt==30 [aw=count_11]
predict predict30 if firstafqt!=30
replace p30=predict30 if firstafqt!=30
drop predict30
rename p30 predict30
drop a f

gen a=10
replace a=1 if firstafqt==49
sort a
gen f=_n if a==1
gen p49=.
sum f
local aa=r(max)
quietly forvalues a = 1/`aa' {
	reg earn_11_19 male i.firstyear i.bage_days educ_lessthanhs educ_inhs educ_ged  educ_some_coll educ_coll_grad     i.bemp2000 i.bsingleparent_share1990 i.bpoor_share1990 i.bmed  if  firstafqt==49 & f!=`a' [aw=count_11]
	predict predict49
	replace p49=predict49 if f==`a'
	drop predict49
}

quietly  reg earn_11_19 male i.firstyear i.bage_days educ_lessthanhs educ_inhs educ_ged  educ_some_coll educ_coll_grad    i.bemp2000 i.bsingleparent_share1990 i.bpoor_share1990 i.bmed   if  firstafqt==49 [aw=count_11]
predict predict49 if firstafqt!=49
replace p49=predict49 if firstafqt!=49
drop predict49
rename p49 predict49
drop a f

quietly foreach a in predict49 {
	sum `a' if earn_11_19!=. & inrange(firstafqt,31,68) [aw=count_11_19]
	replace `a'=(`a'-r(mean))/r(sd)
}

quietly foreach a in predict30 {
	sum `a' if earn_11_19!=. & inrange(firstafqt,12,49) [aw=count_11_19]
	replace `a'=(`a'-r(mean))/r(sd)
}

*main effect rf
sum black if earn_11_19!=. & inrange(firstafqt,12,49) [aw=count_11_19]
replace black=(black-r(mean))

ivregress 2sls earn_11_19 inst31  predict30  c.predict30#c.k31  c.predict30#c.instk31 c.predict30#c.k31_2 c.predict30#c.instk31_2 k31  instk31 k31_2 instk31_2 i.quarter  black  c.black#c.k31  c.black#c.instk31 c.black#c.k31_2 c.black#c.instk31_2 ( c.access#c.black = c.black#c.inst31 ) if inrange(firstafqt,12,49)  & missc==0 & earn_11_19!=. [aw=count_11_19],  r
ivregress 2sls earn_11_19 inst31 predict30  c.predict30#c.k31  c.predict30#c.instk31 c.predict30#c.k31_2 c.predict30#c.instk31_2 k31  instk31 k31_2 instk31_2 i.quarter  black  c.black#c.k31  c.black#c.instk31 c.black#c.k31_2 c.black#c.instk31_2 ( c.access#c.black c.access#c.predict30= c.black#c.inst31 c.inst31#c.predict30) if inrange(firstafqt,12,49)  & missc==0 & earn_11_19!=. [aw=count_11_19], r

sum black if earn_11_19!=. & inrange(firstafqt,31,68) [aw=count_11_19]
replace black=(black-r(mean))

ivregress 2sls earn_11_19 inst50  predict49  c.predict49#c.k50  c.predict49#c.instk50 c.predict49#c.k50_2 c.predict49#c.instk50_2 k50  instk50 k50_2 instk50_2 i.quarter  black  c.black#c.k50  c.black#c.instk50 c.black#c.k50_2 c.black#c.instk50_2 ( c.access#c.black  = c.black#c.inst50  ) if inrange(firstafqt,31,68)  & missc==0 & earn_11_19!=. [aw=count_11_19], r
ivregress 2sls earn_11_19 inst50 predict49  c.predict49#c.k50  c.predict49#c.instk50 c.predict49#c.k50_2 c.predict49#c.instk50_2 k50  instk50 k50_2 instk50_2 i.quarter  black  c.black#c.k50  c.black#c.instk50 c.black#c.k50_2 c.black#c.instk50_2 ( c.access#c.black c.access#c.predict49 = c.black#c.inst50  c.predict49#c.inst50) if inrange(firstafqt,31,68)  & missc==0 & earn_11_19!=. [aw=count_11_19], r

drop predict*

gen a=10
replace a=1 if firstafqt==30
sort a
gen f=_n if a==1
gen p30=.
sum f
local aa=r(max)
quietly forvalues a = 1/`aa' {
	reg earn_11_19  i.firstyear i.btot_inc young file poor is_dep dep male i.bage_days educ_lessthanhs educ_inhs educ_ged  educ_some_coll educ_coll_grad    i.bemp2000 i.bsingleparent_share1990 i.bpoor_share1990 i.bmed   if  firstafqt==30 & f!=`a' [aw=count_11]
	predict predict30 
	replace p30=predict30 if f==`a'
	drop predict30
}
quietly  reg earn_11_19  i.firstyear i.btot_inc young file poor is_dep dep male i.bage_days educ_lessthanhs educ_inhs educ_ged  educ_some_coll educ_coll_grad    i.bemp2000 i.bsingleparent_share1990 i.bpoor_share1990 i.bmed   if  firstafqt==30 [aw=count_11]
predict predict30 if firstafqt!=30
replace p30=predict30 if firstafqt!=30
drop predict30
rename p30 predict30
drop a f

gen a=10
replace a=1 if firstafqt==49
sort a
gen f=_n if a==1
gen p49=.
sum f
local aa=r(max)
quietly forvalues a = 1/`aa' {
	reg earn_11_19  i.firstyear i.btot_inc young file poor is_dep dep male i.bage_days educ_lessthanhs educ_inhs educ_ged  educ_some_coll educ_coll_grad     i.bemp2000 i.bsingleparent_share1990 i.bpoor_share1990 i.bmed  if  firstafqt==49 & f!=`a' [aw=count_11]
	predict predict49
	replace p49=predict49 if f==`a'
	drop predict49
}

quietly  reg earn_11_19  i.firstyear i.btot_inc young file poor is_dep dep male i.bage_days educ_lessthanhs educ_inhs educ_ged  educ_some_coll educ_coll_grad    i.bemp2000 i.bsingleparent_share1990 i.bpoor_share1990 i.bmed   if  firstafqt==49 [aw=count_11]
predict predict49 if firstafqt!=49
replace p49=predict49 if firstafqt!=49
drop predict49
rename p49 predict49
drop a f

local vars predict49
quietly foreach a of local vars {
	sum `a' if earn_11_19!=. & inrange(firstafqt,31,68) [aw=count_11_19]
	replace `a'=(`a'-r(mean))/r(sd)
}

local vars  predict30
quietly foreach a of local vars {
	sum `a' if earn_11_19!=. & inrange(firstafqt,12,49) [aw=count_11_19]
	replace `a'=(`a'-r(mean))/r(sd)
}

sum black if earn_11_19!=. & inrange(firstafqt,12,49) [aw=count_11_19]
replace black=(black-r(mean))

ivregress 2sls earn_11_19 inst31  predict30  c.predict30#c.k31  c.predict30#c.instk31 c.predict30#c.k31_2 c.predict30#c.instk31_2 k31  instk31 k31_2 instk31_2 i.quarter  black  c.black#c.k31  c.black#c.instk31 c.black#c.k31_2 c.black#c.instk31_2 ( c.access#c.black = c.black#c.inst31 ) if inrange(firstafqt,12,49)  & missc==0 & earn_11_19!=. [aw=count_11_19],  r
ivregress 2sls earn_11_19 inst31 predict30  c.predict30#c.k31  c.predict30#c.instk31 c.predict30#c.k31_2 c.predict30#c.instk31_2 k31  instk31 k31_2 instk31_2 i.quarter  black  c.black#c.k31  c.black#c.instk31 c.black#c.k31_2 c.black#c.instk31_2 ( c.access#c.black c.access#c.predict30= c.black#c.inst31 c.inst31#c.predict30) if inrange(firstafqt,12,49)  & missc==0 & earn_11_19!=. [aw=count_11_19], r

sum black if earn_11_19!=. & inrange(firstafqt,31,68) [aw=count_11_19]
replace black=(black-r(mean))

ivregress 2sls earn_11_19 inst50  predict49  c.predict49#c.k50  c.predict49#c.instk50 c.predict49#c.k50_2 c.predict49#c.instk50_2 k50  instk50 k50_2 instk50_2 i.quarter  black  c.black#c.k50  c.black#c.instk50 c.black#c.k50_2 c.black#c.instk50_2 ( c.access#c.black  = c.black#c.inst50  ) if inrange(firstafqt,31,68)  & missc==0 & earn_11_19!=. [aw=count_11_19], r
ivregress 2sls earn_11_19 inst50 predict49  c.predict49#c.k50  c.predict49#c.instk50 c.predict49#c.k50_2 c.predict49#c.instk50_2 k50  instk50 k50_2 instk50_2 i.quarter  black  c.black#c.k50  c.black#c.instk50 c.black#c.k50_2 c.black#c.instk50_2 ( c.access#c.black c.access#c.predict49 = c.black#c.inst50  c.predict49#c.inst50) if inrange(firstafqt,31,68)  & missc==0 & earn_11_19!=. [aw=count_11_19], r

log close
